#!/bin/bash
#自动备份策略
#file:///hdd/test/built-docs-master/raw/en/elasticsearch/reference/master/getting-started-snapshot-lifecycle-management.html
#SLM

#DELTE
curl -XDELETE "http://localhost:9200/_snapshot/backup?pretty"
curl -XDELETE "http://localhost:9200/_slm/policy/nightly-snapshots?pretty"

#创建快照仓库和策略
curl -XPUT "http://localhost:9200/_snapshot/backup?pretty" -H 'Content-Type: application/json' -d'
{
    "type": "fs",
    "settings": {
        "location": "/opt/backup",
        "max_snapshot_bytes_per_sec" : "50mb",
        "max_restore_bytes_per_sec" : "50mb",
        "compress" : true
    }
}
'
#每天UTC 2:30 AM执行
#具体含义在 http://localhost:5601/app/kibana#/management/elasticsearch/snapshot_restore/policies 可以查看
curl -XPUT "http://localhost:9200/_slm/policy/nightly-snapshots?pretty" -H 'Content-Type: application/json' -d'
{
  "schedule": "0 30 2 * * ?", 
  "name": "<nightly-snap-{now/d}>", 
  "repository": "backup", 
  "config": { 
    "indices": ["test*"] 
  },
  "retention": { 
    "expire_after": "30d", 
    "min_count": 5, 
    "max_count": 50 
  }
}
'

#强制执行和检查
curl -XDELETE "http://localhost:9200/_snapshot/backup/nightly-snap*?pretty"
rm -rf ../esdata/backup/*

# Instead of waiting for the policy to run, tell SLM to take a snapshot using the configuration right now instead of waiting for crontab 
curl -XPOST "http://localhost:9200/_slm/policy/nightly-snapshots/_execute?pretty"
#After forcing the nightly-snapshots policy to run, you can retrieve the policy to get success or failure information.
curl -XGET "http://localhost:9200/_slm/policy/nightly-snapshots?human&pretty"

#看快照列表
curl -XGET "http://localhost:9200/_snapshot/backup/_all?pretty"